package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Orders;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Tang
 * @date 2023/2/25
 */
@RequestMapping("/order")
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        //1.调用service的提交订单方法
        ordersService.submit(orders);
        //2.返回结果
        return R.success("提交订单成功！");
    }

    /***
     * 订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pageOrderByUser(@RequestParam(defaultValue = "1") Long page
            , @RequestParam(defaultValue = "1") Long pageSize) {
        Page<OrdersDto> ordersDtoList = ordersService.pageOrderByUser(page, pageSize);
//        Page<Orders> ordersPage = ordersService.page(new Page<Orders>(page, pageSize), new LambdaQueryWrapper<Orders>().eq(Orders::getUserId, BaseContext.getCurrentId()));
        return R.success(ordersDtoList);
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        ordersService.again(orders);
        return R.success("新增成功");
    }

    /**
     * 订单详情分页查询
     * @param page
     * @param pageSize
     * @return
     */
//    @GetMapping("page")
//    public R<Page<OrderDetail>> orderPage(@RequestParam(defaultValue = "1") Integer page,
//
//                                @RequestParam(defaultValue = "10") Integer pageSize) {
//        Page page1= orderDetailService.orderPage(page,pageSize);
//
//        return R.success(page1);
//    }

    /**
     * 订单状态查询及订单条件查询
     *
     * @param page
     * @param pageSize
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("page")
    public R<Page<OrdersDto>> state(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false) String number,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime
    ) {
        Page<OrdersDto> orders = orderDetailService.getOrders(page, pageSize, number, beginTime, endTime);
        return R.success(orders);


    }

}
